<template>
  <div class="setps-container">
    <a-steps :current="current" class="setps">
      <a-step v-for="item in steps" :key="item.title" :title="item.title" />
    </a-steps>
    <div class="steps-content">
      <basic-detail :form="form" @next="next" v-if="current === 0" />
      <sale-detail :form="form" @next="next" @prev="prev" v-else-if="current === 1" />
    </div>
  </div>
</template>

<script>
import BasicDetail from '../../components/addShop/basicDetail.vue';
import SaleDetail from '../../components/addShop/saleDetail.vue';
import api from '@/api/prodouct';

const key = 'this.$store.state.user.appkey';
export default {
  data() {
    return {
      current: 0,
      form: {
        title: '',
        desc: '',
        category: '',
        c_items: [],
        tags: [],
        price: 0,
        price_off: 0,
        unit: '',
        inventory: 0,
        images: [],
      },
      steps: [
        {
          title: '填写商品基本信息',
        },
        {
          title: '填写商品销售信息',
        },
      ],
    };
  },
  components: {
    BasicDetail,
    SaleDetail,
  },
  created() {
    const { id } = this.$route.params;
    if (id) {
      // 代表有这个id值 为编辑
      api.detail(id).then((res) => {
        this.form = res.data.data;
      });
    }
  },
  methods: {
    next(form) {
      this.form = {
        ...this.form,
        form,
      };
      if (this.current === 1) {
        if (this.$route.params.id) {
          api.edit(this.form).then(() => {
            this.$message.success('修改完成');
            setTimeout(() => {
              this.$router.push({
                name: 'ProdouctList',
              });
            }, 2000);
          });
        } else {
          api.add(this.form).then((res) => {
            if (res.data.status === 'success') {
              this.openMessage();
              setTimeout(() => {
                this.$router.push({
                  name: 'ProdouctList',
                });
              }, 2000);
            }
          });
        }
      } else {
        this.current += 1;
      }
    },
    prev() {
      this.current -= 1;
    },
    openMessage() {
      this.$message.loading({ content: 'Loading...', key });
      setTimeout(() => {
        this.$message.success({ content: '新增成功', key, duration: 2 });
      }, 1000);
    },
  },
};
</script>

<style lang="less" scoped>
.setps-container::-webkit-scrollbar {
  display: none;
}
.setps-container {
  width: 100%;
  .setps {
    width: 50%;
    margin: 20px auto;
  }
  .steps-content {
    margin-top: 16px;
    border: 1px dashed #e9e9e9;
    border-radius: 6px;
    background-color: #fafafa;
    min-height: 200px;
    text-align: center;
    padding-top: 80px;
  }

  .steps-action {
    margin-top: 24px;
  }
}
</style>
